//////////////////////////////////////////////////////
// Curiosity.cpp
//////////////////////////////////////////////////////

#include <windows.h>
#include <dxerr8.h>
#include "MyEngine.h"
#include "MyCursor.h"

// Function prototypes.
LRESULT WINAPI WndProc(HWND hWnd, UINT msg,
    WPARAM wParam, LPARAM lParam);
void RegisterWindowClass(HINSTANCE hInstance);
void CreateAppWindow(HINSTANCE hInstance);
WPARAM StartMessageLoop();

// Global variables.
HWND g_hWnd;
CMyEngine g_Engine("TrainingGrounds.map", TRAININGGROUNDS, 723, 729);
MyCursor g_Cursor(817, 15, 0);

//////////////////////////////////////////////////////
// WinMain()
//////////////////////////////////////////////////////
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
{
    RegisterWindowClass(hInstance);
    CreateAppWindow(hInstance);
    g_Engine.SetWindowHandle(g_hWnd);
    g_Engine.StartAnimationTimer();
    HRESULT hResult = g_Engine.GetCDirectSoundObj()->InitDirectSound();
    if (hResult == DS_OK)
        HRESULT hResult = g_Engine.GetCDirect3DObj()->InitD3D();
    if (hResult == D3D_OK)
    {
        ShowWindow(g_hWnd, SW_SHOWDEFAULT);
        UpdateWindow(g_hWnd);
        StartMessageLoop();
    }
    else
    {
        char* msg = g_Engine.GetCDirect3DObj()->GetErrorString();
        DXTRACE_ERR(msg, hResult);
    }
    return 0;
}

//////////////////////////////////////////////////////
// WndProc()
//////////////////////////////////////////////////////
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_CREATE:
        return 0;

    case WM_DESTROY:
        PostQuitMessage( 0 );
        return 0;

    case WM_PAINT:
        ValidateRect(g_hWnd, NULL);
        return 0;

    case WM_KEYDOWN:
        g_Engine.HandleKeys(wParam);
        return 0;

    case WM_TIMER:
        g_Engine.HandleTimer();
        return 0;
    }
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

//////////////////////////////////////////////////////
// RegisterWindowClass()
//////////////////////////////////////////////////////
void RegisterWindowClass(HINSTANCE hInstance)
{
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0; 
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "Curiosity";
    wc.hIconSm = NULL;

    RegisterClassEx(&wc);
}

//////////////////////////////////////////////////////
// CreateAppWindow()
//////////////////////////////////////////////////////
void CreateAppWindow(HINSTANCE hInstance)
{
    g_hWnd = CreateWindowEx(
        NULL,
        "Curiosity",
        "Curiosity", 
        WS_OVERLAPPEDWINDOW,
        100,
        100,
        648,
        514,
        GetDesktopWindow(),
        NULL,
        hInstance,
        NULL);
}

//////////////////////////////////////////////////////
// StartMessageLoop()
//////////////////////////////////////////////////////
WPARAM StartMessageLoop()
{
    MSG msg;
    while(1)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                break;
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            // Use idle time here.
            g_Engine.ProcessGame();
        }
    }
    return msg.wParam;
}
